package com.example.myapplication4;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNav;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bottomNav = findViewById(R.id.bottom_nav);
        fragmentManager = getSupportFragmentManager();

        // 默认显示第一个Fragment
        replaceFragment(new AnalysisFragment());

        // 底部导航栏切换事件
        bottomNav.setOnItemSelectedListener(item -> {
            if (item.getItemId() == R.id.menu_analysis) {
                replaceFragment(new AnalysisFragment());
                return true;
            } else if (item.getItemId() == R.id.menu_donation) {
                replaceFragment(new DonationFragment());
                return true;
            } else if (item.getItemId() == R.id.menu_bus) {
                replaceFragment(new BusFragment());
                return true;
            } else if (item.getItemId() == R.id.menu_party) {
                replaceFragment(new PartyFragment());
                return true;
            }
            return false;
        });
    }

    // 替换Fragment的方法
    private void replaceFragment(Fragment fragment) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();
    }
}